import xml.etree.ElementTree as ET
#打开xml文件
tree = ET.parse("test.xml")
#获得第一层根
root = tree.getroot()
#输出属性。
print(root.tag, ":", root.attrib)  # 打印根元素的tag和属性

#在data中寻找country
for country in root:
    #在country中寻找city
    for city in country:
        #在city中把各个标签输出出来。
        for j in range(len(city)):
            print(city[j].tag,':',city[j].text)




#注释为xml文件样式。

# <?xml version="1.0"?>
# <data>
#     <country name="Liechtenstein">
#         <city>
#             <rank updated="yes">5</rank>
#             <year>2011</year>
#             <gdppc>59900</gdppc>
#             <neighbor name="Malaysia" direction="N"/>
#
#         </city>
#
#                 <city>
#             <rank updated="yes">5</rank>
#             <year>2011</year>
#             <gdppc>59900</gdppc>
#             <neighbor name="Malaysia" direction="N"/>
#
#             </city>
#
#     </country>
#
#
#
#         <country name="Liechtenstein">
#         <city>
#             <rank updated="yes">5</rank>
#             <year>2011</year>
#             <gdppc>59900</gdppc>
#             <neighbor name="Malaysia" direction="N"/>
#
#         </city>
#
#                 <city>
#             <rank updated="yes">5</rank>
#             <year>2011</year>
#             <gdppc>59900</gdppc>
#             <neighbor name="Malaysia" direction="N"/>
#
#             </city>
#
#     </country>
#
#         <country name="Liechtenstein">
#         <city>
#             <rank updated="yes">5</rank>
#             <year>2011</year>
#             <gdppc>59900</gdppc>
#             <neighbor name="Malaysia" direction="N"/>
#
#         </city>
#
#                 <city>
#             <rank updated="yes">5</rank>
#             <year>2011</year>
#             <gdppc>59900</gdppc>
#             <neighbor name="Malaysia" direction="N"/>
#
#             </city>
#
#     </country>
#
# </data>